page.form = {};
page.form.init = function(){
	$('#birthday').datepicker({format: 'yyyy-mm-dd'});
	$("#userImageFile").fileinput({
		  uploadUrl  : contextPath + "/common/uploadUserImage.htm",
		  showUpload : false,
          showRemove : false,
          uploadAsync:false,
          language : 'zh',
          allowedPreviewTypes: ['image'],
          allowedFileTypes: ['image'],
          allowedFileExtensions:  ['jpg', 'png'],
          maxFileSize : 2000,
          uploadExtraData: function(previewId, index) {   //额外参数的关键点
              var obj = {};
              obj.userId = $("#userId").val();
              return obj;
          }
	}).on("filebatchuploadsuccess", function(event, data) {
		if(data.response.success){
			 layer.msg(data.response.msg);
			 layer.closeAll();
		}else{
			layer.msg(data.response.msg);
		}
	}).on('filebatchuploaderror', function(event, data, msg) {
		layer.msg("保存失败");
	});
	$("#userImage").on("click",function(){
		$("#userImageFile").click();
		$("#userImageFile").on("change",function(){
			var $file = $(this);
			var fileObj = $file[0];
			var windowURL = window.URL || window.webkitURL;
			var dataURL;
			var $img = $("#userImage");
			 
			if(fileObj && fileObj.files && fileObj.files[0]){
			dataURL = windowURL.createObjectURL(fileObj.files[0]);
			$img.attr('src',dataURL);
			}else{
			dataURL = $file.val();
			var imgObj = document.getElementById("userImage");
			// 两个坑:
			// 1、在设置filter属性时，元素必须已经存在在DOM树中，动态创建的Node，也需要在设置属性前加入到DOM中，先设置属性在加入，无效；
			// 2、src属性需要像下面的方式添加，上面的两种方式添加，无效；
			imgObj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
			imgObj.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = dataURL;
			 
			}
		});
	});
}
page.form.event = {
	submit : function(dialog, callback,index) {
		// 校验
		$('#mainForm').validate();
		var validate = $('#mainForm').valid();
		if (validate) {
			var data = $('#mainForm').serialize() // 要提交的表单,必须使用name属性
			$.dataPost(contextPath + "/common/updateProfile.htm", data,function(result) {
				if (result.success) {
					if (callback)callback();
					$("#userImageFile").fileinput("upload");  
				} else {
					layer.msg(result.msg);
				}
			});
		}

	}
}
